package com.lzd.framework.springsecurity.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lzd.framework.springsecurity.model.UserInfo;

@RestController
@RequestMapping("/admin")
public class UserController {
	
	private BCryptPasswordEncoder passwordEncoder; 
	
	public UserController(){
		passwordEncoder = new BCryptPasswordEncoder();
	}

    @GetMapping("/user/{username}")
    @PreAuthorize("@pms.hasPermission('admin:user:info')")    
    public ResponseEntity<UserInfo> getUserInfo(@PathVariable String username) {

        String  user  = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String email = user+ "@test.com";
        UserInfo userInfo = new UserInfo();
        userInfo.setName(user);
        userInfo.setEmail(email);
        return ResponseEntity.ok(userInfo);
    }
    
    @GetMapping("/passwordencoder/{password}")
    @PreAuthorize("@pms.hasPermission('admin:user:passwordencoder')")    
    public ResponseEntity<String> getEncryStr(@PathVariable String password) {
    	String result= passwordEncoder.encode(password);
        return ResponseEntity.ok("{bcrypt}"+result);
    }    

}